import {ref} from "vue";
import {defineStore} from 'pinia';

export const usePermissionStore = defineStore('permissionStore', () => {
    const routeAddModalVisible = ref(false)    //是否显示路由编辑框，应用于菜单编辑页面
    //路由表单，用于编辑路由
    let routeForm = ref({
        name: '',
        path: '',
        componentPath: '',
        meta: {
            title: '',
            hideInMenu: '0',
            icon: ''
        }
    })


    function openRouteAddModal(route, parentId) {
        _setRouteForm(route, parentId)
        routeAddModalVisible.value = true;
    }

    function _setRouteForm(route, parentId) {
        if (!route) {
            // 添加路由，默认值
            routeForm.value = {
                meta: {}
            }
            if (parentId) {
                routeForm.value.parentId = parentId
            }
            return
        }
        // 为this.routeForm赋值
        Object.keys(routeForm.value).forEach(key => {
            if (route.hasOwnProperty(key)) {
                routeForm.value[key] = route[key]
            }
        })
    }



    return {
        routeAddModalVisible,
        routeForm,
        openRouteAddModal,
    }
})